import request from "@/utils/request";

const HTTP_BASE_URL = "/biz/mqtt";

// Topic 接口定义
export interface Topic {
	name: string;
	prefix: string;
	topic: string;
	qos: '0' | '1' | '2';
	remark: string;
}

// MQTT客户端表单接口
export interface MqttClientForm {
	id?: string;
	name?: string;
	prefix?: string;
	host?: string;
	username?: string;
	password?: string;
	timeout?: number;
	keepalive?: number;
	type?: string;  // 0: 消费者, 1: 生产者, 2: 消费者&生产者
	topics?: string;         // JSON字符串
	publishTopics?: string;  // JSON字符串
	createTime?: Date;
	updateTime?: Date;
	createUserId?: string;
	updateUserId?: string;
}

// 分页查询参数接口
const MqttAPI = {
	/**
	 * 分页获取MQTT客户端列表
	 * @param params 分页查询参数
	 */
	pages(params: any) {
		return request<PageResult<any>>({
			url: `${HTTP_BASE_URL}/pages`,
			method: "get",
			params
		});
	},

	/**
	 * 获取MQTT客户端列表
	 * @param keywords 关键字搜索
	 */
	options(data?: any) {
		return request<any, any>({
			url: `${HTTP_BASE_URL}/options`,
			method: "get",
			params: data
		});
	},

	/**
	 * 保存MQTT客户端
	 * @param data MQTT客户端表单数据
	 */
	save(data: any) {
		return request<any>({
			url: `${HTTP_BASE_URL}/save`,
			method: "post",
			data
		});
	},

	/**
	 * 删除MQTT客户端
	 * @param ids 客户端ID数组
	 */
	delete(ids: string[]) {
		return request<string>({
			url: `${HTTP_BASE_URL}/${ids.join(",")}`,
			method: "delete"
		});
	},

	/**
	 * 测试MQTT客户端连接
	 * @param id 客户端ID
	 */
	testConnection(id: string) {
		return request<boolean>({
			url: `${HTTP_BASE_URL}/test/${id}`,
			method: "post"
		});
	},

	/**
	 * 启动MQTT客户端
	 * @param id 客户端ID
	 */
	startClient(id: string) {
		return request<boolean>({
			url: `${HTTP_BASE_URL}/start/${id}`,
			method: "post"
		});
	},

	/**
	 * 停止MQTT客户端
	 * @param id 客户端ID
	 */
	stopClient(id: string) {
		return request<boolean>({
			url: `${HTTP_BASE_URL}/stop/${id}`,
			method: "post"
		});
	}
};

export default MqttAPI;
